{extend name="common/base"/}
<!-- 主体 -->
{block name="body"}

<div class="p-3">
	<div>
		<form class="layui-form gg-form-bar border-x border-t" lay-filter="barsearchform">
			<div class="layui-input-inline" style="width:300px;">
				<input type="text" name="keywords" placeholder="ID/目录名称" class="layui-input" autocomplete="off" />
			</div>
			<div class="layui-input-inline">
				<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">提交搜索</button>
				<button type="reset" class="layui-btn layui-btn-reset" lay-filter="clear">清空</button>
			</div>
		</form>
		<table class="layui-hide" id="test" lay-filter="test"></table>
	</div>
</div>
<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-sm add-menu">+ 添加目录</button>
	</div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const moduleInit = ['tool', 'treeGrid'];
	function gouguInit() {
		var treeGrid = layui.treeGrid,table = layui.table, tool = layui.tool, form = layui.form,tree = layui.tree;

		//目录列表
		layui.pageTable = table.render({
			elem: '#test',
			title: '数据列表',
			toolbar: '#toolbarDemo'
			, url: "/matter/matter_directory/index"
			, cellMinWidth: 80
			, page: true//开启分页
			,limit: 16
			, cols: [[
				 { field: 'id', width: 100, title: 'ID号', align: 'center' }
				, { field: 'pid', title: '上级目录ID',width: 100, align: 'center'}
				, { field: 'name', title: '目录名称'}
				, { field: 'unit_name', title: '所属机构',width: 240, align: 'center', templet: function (d) {
						let str = '';
						if(d.unit){
							str += d.unit.name;
						}
					return str;
				} }
				, { field: 'status', title: '状态',width: 40, align: 'center', templet: function (d) {
					if(d.status == 1){
						return '<span class="layui-badge layui-bg-green">发布</span>';
					}else{
						return '<span class="layui-badge">暂停</span>';
					}
				} }
				, { width:150, title: '操作', align: 'center', templet: function (d) {

						var html = '<span class="layui-btn-group"><button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</button><button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button></span>';
						return html;
					}
				}
			]]
		});
		
		//表头工具栏事件
		$('body').on('click','.add-menu', function(){
			tool.side("/matter/matter_directory/add");
			return;
		});

		//操作按钮
		table.on('tool(test)', function (obj) {
			if (obj.event === 'add') {
				tool.side("/matter/matter_directory/add?pid="+obj.data.id);
				return;
			}
			if (obj.event === 'edit') {
				tool.side("/matter/matter_directory/add?id="+obj.data.id);
				return;
			}
			if (obj.event === 'del') {
				layer.confirm('确定要删除吗?', {icon: 3, title:'提示'}, function(index){
					let callback = function (e) {
						layer.msg(e.msg);
						if (e.code == 0) {
							obj.del();
						}
					}
					tool.delete("/matter/matter_directory/delete", { id: obj.data.id }, callback);
					layer.close(index);
				});
			}
		});

		//监听搜索提交
		form.on('submit(webform)', function (data) {
			layui.pageTable.reload({
				where: {
					keywords: data.field.keywords,
				},
				page: {
					curr: 1
				}
			});
			console.log()
			return false;
		});
		$('[lay-filter="clear"]').on('click',function(){
			setTimeout(function(){
				$('[lay-filter="webform"]').click();
			},10)
		})

	}
</script>
{/block}
<!-- /脚本 -->